import requests
import json


def call_zhipu_api(messages, model="glm-4.6"):
    url = "https://open.bigmodel.cn/api/coding/paas/v4/chat/completions"

    headers = {
        "Authorization": "Bearer 68a5728c8a8d444ebbb77daff8e754b3.SXGC3K2XHsXH2xbf",
        "Content-Type": "application/json"
    }

    data = {
        "model": model,
        "messages": messages,
        "temperature": 1.0,
        "thinking": {
            "type": "enabled"
        }
    }

    response = requests.post(url, headers=headers, json=data)

    if response.status_code == 200:
        return response.json()
    else:
        raise Exception(f"API调用失败: {response.status_code}, {response.text}")


# 使用示例
messages = [
    {"role": "user", "content": "你好，请介绍一下自己"}
]

result = call_zhipu_api(messages)
print(result['choices'][0]['message']['content'])
